<template>
  <van-row>
    <van-nav-bar
      title="员工管理"
      left-text="返回"
      left-arrow
      @click-left="$router.push('/')"
      @click-right="handleAction"
    >
      <template #right>
        <van-icon name="plus" size="20" />
      </template>
    </van-nav-bar>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-search v-model="page.query.keyword" show-action placeholder="请输入关键字" @search="handleSearch">
        <template #action>
          <div @click="handleSearch">搜索</div>
        </template>
      </van-search>
      <van-list
        v-model="tableLoading"
        :finished="finished"
        finished-text="没有更多了"
        @load="doPage"
      >
        <van-cell v-for="(item,index) in tableData" :key="index" is-link @click="toDetail(item)">
          <template #title>
            <div class="title">
              <div v-html="highlightText(item.employeeName, page.query.keyword)" />
            </div>
            <div class="title right primary">
              <position-state-tag v-model="item.positionState" />
              <sex-tag v-model="item.sex" />
            </div>
          </template>
          <template #label>
            <div v-html="highlightText(item.employeeNo, page.query.keyword)" />
          </template>
        </van-cell>

      </van-list>
    </van-pull-refresh>
    <van-popup v-model="showDetail" position="right" :style="{ width: '100%', height: '100%' }">
      <employee-detail-module ref="detailModule" @close="handleClose" />
    </van-popup>
    <van-popup v-model="showSubmit" position="right" :style="{ width: '100%', height: '100%' }">
      <employee-submit-module ref="submitModule" @close="handleClose" />
    </van-popup>
  </van-row>
</template>
<script>
import { selectEmployeePage } from '@/api/employee'
import { highlightText } from '@/utils/util'
import PositionStateTag from '@/components/tag/PositionStateTag.vue'
import SexTag from '@/components/tag/SexTag.vue'
import EmployeeSubmitModule from '@/views/employee/employeeSubmit.vue'
import EmployeeDetailModule from '@/views/employee/employeeDetail.vue'

export default {
  name: 'Employee',
  components: { EmployeeDetailModule, EmployeeSubmitModule, SexTag, PositionStateTag },
  data() {
    return {
      showSubmit: false,
      showDetail: false,
      tableLoading: false,
      finished: false,
      refreshing: false,
      tableData: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          keyword: ''
        }
      }
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    highlightText,
    doPage() {
      this.tableLoading = true
      selectEmployeePage(this.page).then(resp => {
        if (resp.success) {
          if (this.refreshing) {
            this.tableData = []
            this.refreshing = false
          }
          this.tableData = [...this.tableData, ...resp.data.records]
          this.page.total = resp.data.total
          this.finished = this.tableData.length >= resp.data.total
        }
      }).catch(() => {
        this.finished = true
      }).finally(() => {
        this.tableLoading = false
        this.page.page++
      })
    },
    handleSearch() {
      this.tableData = []
      this.page.page = 1
      this.finished = false
      this.doPage()
    },
    onRefresh() {
      this.page.page = 1
      this.doPage()
    },
    toDetail(row) {
      this.showDetail = true
      this.$nextTick(() => {
        this.$refs.detailModule.init(row.id)
      })
    },
    handleAction() {
      this.showSubmit = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    handleClose() {
      this.showSubmit = false
      this.showDetail = false
      this.handleSearch()
    }
  }
}
</script>

<style scoped>

</style>
